#include <iostream>
#include <cmath>
using namespace std;

bool g_InvalidInput = false;

bool equal(double m1, double m2)
{
    if (abs(m1 - m2) < 1e-5)
        return true;
    else
        return false;
}

double PowerWithUnsignedExponent_Iteratively(double base, unsigned int exponent)
{
    double result = 1.0;
    for (int i = 1; i <= exponent; ++ i)
        result *= base;
    return result;
}

double PowerWithUnsignedExponent_Recursively(double base, unsigned int exponent)
{
    if (exponent == 0)
        return 1.0;
    if (exponent == 1)
        return base;

    double result = PowerWithUnsignedExponent_Recursively(base, exponent >> 1);
    result *= result;
    if(exponent & 0x1 == 1)
        result *= base;

    return result;
}

double Pow(double base, int exponent)
{
    g_InvalidInput = false;

    if (equal(base, 0.0) && exponent < 0)
    {
        g_InvalidInput = true;
        return 0.0;
    }

    unsigned int absExponent = (unsigned int)(exponent);

    if (exponent < 0)
        absExponent = (unsigned int)(-exponent);
    
    double result = PowerWithUnsignedExponent_Recursively(base, absExponent);
    
    if (exponent < 0)
        result = 1.0 / result;

    return result;
}
int main(int argc, char * argv[])
{
    double base = -3;
    int exponent = 3;
    cout << base << "^" << exponent << " = " << Pow(base, exponent) << endl;
}
